/*
 *    Copyright 2014 Sam Y Ge
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.strx;

import java.util.HashMap;

/**
 * 开始标签
 * 
 * @author <a href="mailto:samuel.y@foxmail.com">Sam Y Ge</a>
 */
class StartTag extends Tag {
	/**
	 * 构造函数，包内调用
	 * 
	 * @param tagStr
	 *            标签原始文本
	 */
	StartTag(String tagStr) {
		super(TagType.START, tagStr);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	void parseName() {
		if (!tagStr.endsWith(Tag.TAG_END)) {
			hasText = true;
		}

		int endIdx = tagStr.indexOf(Tag.TAG_END);
		tagBody = tagStr.substring(1, endIdx).trim();
		int equalIdx = tagBody.indexOf(Tag.EQUAL);
		if (-1 == equalIdx) {
			name = tagBody;
		} else {
			int rightBorder = tagBody.length();
			for (int i = 0; i < rightBorder; i++) {
				char c = tagBody.charAt(i);
				if (c <= ' ') {
					rightBorder = i;
					break;
				}
			}

			name = tagBody.substring(0, rightBorder);
			hasAttr = true;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	void parseAttrs() {
		if (hasAttr) {
			attributes = new HashMap<String, String>(); // 确定存在属性之后再构造该对象

			int equalIdx = 1;
			while (true) {
				equalIdx = tagBody.indexOf(Tag.EQUAL, ++equalIdx);
				if (-1 == equalIdx) {
					break;
				}

				int begin = 0;
				int end = 0;

				// 向前搜索属性名
				boolean foundAttrName = false;
				for (int i = equalIdx - 1; i > 0; --i) {
					char c = tagBody.charAt(i);
					if (c <= ' ') {
						if (foundAttrName) {
							begin = i;
							break;
						}
					} else {
						if (!foundAttrName) {
							foundAttrName = true;
							end = i + 1;
						}
					}
				}

				// 向后搜索属性值
				String name = tagBody.substring(++begin, end);

				char wrap = ' ';
				begin = equalIdx + 1;
				while (begin < tagBody.length()) {
					char c = tagBody.charAt(begin);

					// 忽略属性值之前的所有空格
					if (c == ' ') {
						++begin;
						continue;
					}

					// 判定界定符
					if (c == '\'') {
						++begin;
						wrap = '\'';
					} else if (c == '"') {
						++begin;
						wrap = '"';
					}
					break;
				}

				for (int i = begin; i < tagBody.length(); ++i) {
					char c = tagBody.charAt(i);
					if ((c == wrap) || (c == '>')) {
						end = i;
						break;
					}
				}

				String value = tagBody.substring(begin, end);

				// 放入属性表
				attributes.put(name, value);
			}
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	void parseText() {
		if (hasText) {
			int endIdx = tagStr.indexOf(Tag.TAG_END);
			text = tagStr.substring(endIdx + 1).trim();

			if (0 == text.length()) {
				text = null;
			}
		}
	}

}
